<?php
namespace printing;
use printing\lib\YLYOpenApiClient;
// require_once (__DIR__.'/lib/YLYOpenApiClient.php');
class printShop{
    public function random ($len) {
        $param = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            $s = substr($param, rand(0, strlen($param) - 1), 1);
            $str .= $s;
        }
        return $str;
    }

    public function payprinting($machineCode,$community_name,$room_address,$trade_no,$pay_type,$bill_entry_amount,$gmt_payment,$give_point,$user_name){
        $machineCode = $machineCode;//授权的终端号
        $accessToken = '40552d86657c4bdcae446a287ff754f5';//api访问令牌
        $originId = date('YmdHis', time()).$this->random(18);//商户自定义id
        $timesTamp = time();//当前服务器时间戳(10位)

        $aid = 1;
        $con="支付宝到账".$bill_entry_amount."元赠送积分".$give_point."积分欢迎下次惠顾";
        $content = '<VN>'.$aid.'</VN>';
        $content = '<audio>'.$con.',9,0'.'</audio>';

        $content .= '';//打印内容
        $content .= '<center>番茄积分合作商家</center>';
        $content .= str_repeat('-',32);
        $content .= '<FH>';
        $content .= '付款人: '.$user_name."\n";
        $content .= '支付方式: '.$pay_type."\n";
        $content .= '付款金额: '.$bill_entry_amount."\n";
        $content .= '付款时间: '.$gmt_payment."\n";
        //$content .= '收款单位: '.$property."\n";
        $content .= '</FH>';
        $content .= str_repeat('-',32);
        $api = new YLYOpenApiClient();
        $printing = $api->printIndex($machineCode,$accessToken,$content,$originId,$timesTamp);
        if($printing){
            return '打印成功'; 
        }else{
            return '打印失败'; 
        }
    }
    //线上订单打印
    public function payonlineprinting($machineCode,$community_name,$receive_address,$trade_no,$pay_type,$bill_entry_amount,$gmt_payment,$give_point,$user_name,$detailed,$receive_name,$receive_phone,$shop_name){
        $machineCode = $machineCode;//授权的终端号
        $accessToken = '40552d86657c4bdcae446a287ff754f5';//api访问令牌
        $originId = date('YmdHis', time()).$this->random(18);//商户自定义id
        $timesTamp = time();//当前服务器时间戳(10位)

        $aid = 1;
        $con="支付宝到账".$bill_entry_amount."元赠送积分".$give_point."积分欢迎下次惠顾";
        $content = '<VN>'.$aid.'</VN>';
        $content = '<audio>'.$con.',9,0'.'</audio>';

        $content .= '';//打印内容
        $content .= '<center>番茄积分合作商家</center>';
        $content .= str_repeat('-',32);
        $content .= '<FH>';
         $content .= '店铺名称: '.$shop_name."\n";
        $content .= '付款人: '.$user_name."\n";
        $content .= '支付方式: '.$pay_type."\n";
        $content .= '付款金额: '.$bill_entry_amount."\n";
        $content .= '付款时间: '.$gmt_payment."\n";
        $content .= '收货人: '.$receive_name."\n";
        $content .= '电话: '.$receive_phone."\n";
        $content .= '小区: '.$community_name."\n";
        $content .= '地址: '.$receive_address."\n";
        $content .= '商品详情: '."\n";
        foreach($detailed as $det){
            $content .=str_repeat(' ',10);
            $content .= $det."\n";
        }
        $content .= '</FH>';
        
        
        $content .= str_repeat('-',32);
        $api = new YLYOpenApiClient();
        $printing = $api->printIndex($machineCode,$accessToken,$content,$originId,$timesTamp);
        return $printing;
    }

    public function billprinting($machineCode,$community_name,$room_address,$trade_no,$pay_type,$bill_entry_amount,$gmt_payment,$order_money,$point_money,$detailed,$property_name){
        $machineCode = $machineCode;//授权的终端号
        $accessToken = '40552d86657c4bdcae446a287ff754f5';//api访问令牌
        $originId = date('YmdHis', time()).$this->random(18);//商户自定义id
        $timesTamp = time();//当前服务器时间戳(10位)
        $aid = 1;
        $con=$room_address."支付宝收款".$order_money."元";
        $content = '<VN>'.$aid.'</VN>';
        $content = '<audio>'.$con.',9,0'.'</audio>';

        $content .= '';//打印内容
        $content .= '<center>番茄积分合作商家</center>';
        $content .= str_repeat('-',32);
        $content .= '<FH>';
        $content .= '所属小区: '.$community_name."\n";
        $content .= '门牌号: '.$room_address."\n";
        //$content .= '业主姓名: '.$owner_name."\n";
        $content .= '支付宝订单号: '.$trade_no."\n";
        $content .= '支付方式: '.$pay_type."\n";
        $content .= '实收金额: '.$order_money."\n";
        $content .= '实收金额: '.$bill_entry_amount."\n";
        $content .= '积分金额: '.$point_money."\n";
        $content .= '付款时间: '.$gmt_payment."\n";
        //$content .= '收款单位: '.$property."\n";
        $content .= '</FH>';
        $content .= str_repeat('-',16)."\n";
        $content .= '缴费明细: '."\n";
        foreach($detailed as $det){
            $content .= $det."\n";
        }
        $content .= str_repeat('-',32)."\n";
        $content .= '收款单位: '.$property_name;
        $api = new YLYOpenApiClient();
        $printing = $api->printIndex($machineCode,$accessToken,$content,$originId,$timesTamp);
        return $printing;
        if($printing){
            return '打印成功'; 
        }else{
            return '打印失败'; 
        }
    }
}